

package com.hazelcast.client.impl.protocol.codec.custom;

import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.Generated;

import static com.hazelcast.client.impl.protocol.ClientMessage.BEGIN_FRAME;
import static com.hazelcast.client.impl.protocol.ClientMessage.END_FRAME;
import static com.hazelcast.client.impl.protocol.codec.builtin.CodecUtil.fastForwardToEndFrame;
import static com.hazelcast.client.impl.protocol.codec.builtin.FixedSizeTypesCodec.*;

@SuppressWarnings("unused")
@Generated("9029411884963bf7046dcfb64fd1f86b")
public final class MemberVersionCodec {
    private static final int MAJOR_FIELD_OFFSET = 0;
    private static final int MINOR_FIELD_OFFSET = MAJOR_FIELD_OFFSET + BYTE_SIZE_IN_BYTES;
    private static final int PATCH_FIELD_OFFSET = MINOR_FIELD_OFFSET + BYTE_SIZE_IN_BYTES;
    private static final int INITIAL_FRAME_SIZE = PATCH_FIELD_OFFSET + BYTE_SIZE_IN_BYTES;

    private MemberVersionCodec() {
    }

    public static void encode(ClientMessage clientMessage, com.hazelcast.version.MemberVersion memberVersion) {
        clientMessage.add(BEGIN_FRAME.copy());

        ClientMessage.Frame initialFrame = new ClientMessage.Frame(new byte[INITIAL_FRAME_SIZE]);
        encodeByte(initialFrame.content, MAJOR_FIELD_OFFSET, memberVersion.getMajor());
        encodeByte(initialFrame.content, MINOR_FIELD_OFFSET, memberVersion.getMinor());
        encodeByte(initialFrame.content, PATCH_FIELD_OFFSET, memberVersion.getPatch());
        clientMessage.add(initialFrame);

        clientMessage.add(END_FRAME.copy());
    }

    public static com.hazelcast.version.MemberVersion decode(ClientMessage.ForwardFrameIterator iterator) {
        // begin frame
        iterator.next();

        ClientMessage.Frame initialFrame = iterator.next();
        byte major = decodeByte(initialFrame.content, MAJOR_FIELD_OFFSET);
        byte minor = decodeByte(initialFrame.content, MINOR_FIELD_OFFSET);
        byte patch = decodeByte(initialFrame.content, PATCH_FIELD_OFFSET);

        fastForwardToEndFrame(iterator);

        return new com.hazelcast.version.MemberVersion(major, minor, patch);
    }
}
